<?php

namespace app\behaviors;

use app\events\ApplyNotifyEvent;
use app\models\ActivityApply;
use app\models\Sms;
use yii\base\Behavior;
use yii\helpers\Json;

class ApplyNotifyBehavior extends Behavior {

    public function events() {
        return [
            ActivityApply::EVENT_APPLY_NOTIFY => 'notify'
        ];
    }

    public function notify(ApplyNotifyEvent $event) {
        $model = $event->activityApply;
        $activity = $event->activity;

        \Yii::info('------------ApplyNotifyEvent------------');
        if ($model->getIsPending()) {
            return;
        }

        $url = \Yii::$app->request->getHostInfo();
        $times = $activity->formatTime();
        $title = $activity->title;
        $address = $activity->getFullAddress();

        $tpl = $model->getIsAllow() ? '39137' : '39138';
        $params = [
            ['name' => 'title', 'value' => $title],
            ['name' => 'times', 'value' => $times],
            ['name' => 'address', 'value' => $address],
            ['name' => 'url', 'value' => $url],
        ];

        $sms = new Sms();
        $res = $sms->sendSMS($model->phone, $params, $tpl);

        $sms->saveResult($model->phone, $res['msg'], $tpl, Sms::STATUS_YES);
    }
}